import { router } from "@/router";
import { useUserStore } from "@/store/module/user";

export function loginout(before?: () => void, after?: () => void) {
    console.log('统一处理退出登录');
    before && before(); //执行函数
    const useStore = useUserStore(); //请求pinia
    localStorage.removeItem("token");//删除token
    useStore.updateMenus([]);//清除仓库数据
    useStore.username = "";//用户初始化
    const allroutes = router.getRoutes();//清除动态路由
    console.log('清除动态路由', allroutes);
    allroutes.forEach((item) => {
        if (item.name != "login" && item.name) {
            router.removeRoute(item.name);
        }
    });
    //跳转到login
    router.push("/login");
    after && after();
}